<?php

namespace app\admin\controller\formwork;

use app\common\controller\Backend;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Exception;
use think\Db;
use fast\Tree;
/**
 * 模板管理
 *
 * @icon fa fa-circle-o
 */
class Index extends Backend
{

    /**
     * Broadcast模型对象
     * @var \app\admin\model\Broadcast
     */
    protected $model = null;
    protected $category_model = null;
    protected $category = null;
    protected $groupName;
    protected $grouplist;
    protected $categoryName;
    protected $categorylist;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Formwork;
        $this->category_model = new \app\admin\model\FormworkCategory;
        $this->category = new \app\admin\model\shopro\Category;
        $groupList = collection($this->category_model->where('status', 1)->field('id,pid,name')->select())->toArray();
        Tree::instance()->init($groupList);
        $grouplist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
        $groupName = [];
        foreach ($groupList as $k => $v) {
            $groupName[$v['id']] = $v['name'];
        }
        $this->grouplist = $grouplist;
        $this->groupName = $groupName;
        $this->assign('groupName',$groupName);
        $categorylist  = collection($this->category->where('status','normal')->where('id','>','1')->field('id,name')->select())->toArray();
        $categoryName = [];
        foreach ($categorylist as $k => $v) {
            $categoryName[$v['id']] = $v['name'];
        }
        $this->categoryName = $categoryName;
        $this->categorylist =$categorylist ;
        $this->assign('categoryName',$categoryName);
    }
/**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model->with(['cate','category'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function searchlist(){
        return json($this->categorylist);
    }

    public function select()
    {
        if($this->request->isAjax()) {
            $data = $this->model->where('status', 1)->field('id,title')->select();
            $this->success('模板数据', null, $data);
        }
     
    }
}
